{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-03-11T11:22:21.515705Z",
     "start_time": "2020-03-11T11:22:21.390083Z"
    }
   },
   "outputs": [],
   "source": [
    "import cv2\n",
    "import shutil\n",
    "from tqdm import tqdm_notebook as tqdm\n",
    "from pathlib import Path"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-03-11T11:22:22.811675Z",
     "start_time": "2020-03-11T11:22:22.808721Z"
    }
   },
   "outputs": [],
   "source": [
    "max_size = 1024\n",
    "input_dataset_path = '/hdd0/datasets/ImageHarmonization/HAdobe5k'\n",
    "output_path = f'{input_dataset_path}_resized{max_size}'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-03-11T11:22:23.862552Z",
     "start_time": "2020-03-11T11:22:23.795076Z"
    }
   },
   "outputs": [],
   "source": [
    "input_dataset_path = Path(input_dataset_path)\n",
    "output_path = Path(output_path)\n",
    "\n",
    "assert not output_path.exists()\n",
    "\n",
    "output_path.mkdir()\n",
    "for subfolder in ['composite_images', 'masks', 'real_images']:\n",
    "    (output_path / subfolder).mkdir()\n",
    "\n",
    "for annotation_path in input_dataset_path.glob('*.txt'):\n",
    "    shutil.copy(annotation_path, output_path / annotation_path.name)\n",
    "\n",
    "images_list = sorted(input_dataset_path.rglob('*.jpg'))\n",
    "images_list.extend(sorted(input_dataset_path.rglob('*.png')))\n",
    "\n",
    "for x in tqdm(images_list):\n",
    "    image = cv2.imread(str(x), cv2.IMREAD_UNCHANGED)\n",
    "    new_path = output_path / x.relative_to(input_dataset_path)\n",
    "    \n",
    "    if max(image.shape[:2]) <= max_size:\n",
    "        shutil.copy(x, new_path)\n",
    "        continue\n",
    "    \n",
    "    new_width = max_size\n",
    "    new_height = max_size\n",
    "    scale = max_size / max(image.shape[:2])\n",
    "    if image.shape[0] > image.shape[1]:\n",
    "        new_width = int(round(scale * image.shape[1]))\n",
    "    else:\n",
    "        new_height = int(round(scale * image.shape[0]))\n",
    "    \n",
    "    image = cv2.resize(image, (new_width, new_height), interpolation=cv2.INTER_LANCZOS4)\n",
    "    if x.suffix == '.jpg':\n",
    "        cv2.imwrite(str(new_path), image, [cv2.IMWRITE_JPEG_QUALITY, 90])\n",
    "    else:\n",
    "        cv2.imwrite(str(new_path), image)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.9"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": true
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
